using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
namespace DD.Imaging
{
    public static class ImageManager
    {
        public static void AddImageToProduct(int pid, string filename)
        {
            DD.Data.SQL.ImageManager.AddImageToProduct(pid, filename);
        }

        public static DataSet GetProductImages(int pid)
        {
            return DD.Data.SQL.ImageManager.GetProductImages(pid);
        }

        public static string GetProductDefaultImagePath(int pid)
        {
            return DD.Data.SQL.ImageManager.GetProductDefaultImagePath(pid);
        }

        public static string GetImagePath(int ImageId)
        {
            return DD.Data.SQL.ImageManager.GetImagePath(ImageId);
        }
        public static int GetProductDefaultImageId(int pid)
        {
            return DD.Data.SQL.ImageManager.GetProductDefaultImageId(pid);
        
        }
        public static Bitmap GetDisplayImage(int ImageId, int width, int height)
        {
            //System.Drawing.Image i = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(GetImagePath(ImageId)));
            string Path = GetImagePath(ImageId);
            System.Drawing.Image i = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath("~/ProductImages/"+ Path));
            decimal imageW = Convert.ToDecimal(i.Width);
            decimal imageH = Convert.ToDecimal(i.Height);

            int NewHeight = 0;
            int NewWidth = 0;

            decimal ResizeFactor = 1;
            if (imageW > imageH)
            {
                if (imageW > width)
                {
                    ResizeFactor = imageW / width;
                }
                else
                {

                }
            }
            else
            {
                if (imageH > height)
                {
                    ResizeFactor = Decimal.Divide(Convert.ToDecimal(height), imageH);
                }
                else
                {
                }
            }
            decimal tempH = imageH * ResizeFactor;
            decimal tempW = imageW * ResizeFactor;
            
            NewHeight = Convert.ToInt32(Math.Round(tempH, 0));
            NewWidth = Convert.ToInt32(Math.Round(tempW, 0));

            Bitmap bmp = new Bitmap(NewWidth, NewHeight);
            Graphics.FromImage(bmp).DrawImage(i, 0, 0, NewWidth, NewHeight);
          
            return bmp;


            
        }

        private static bool Thumb_Callback()
        {
            return true;
        }
    }
}
